<template>
  <div class="comments">
    <!--  -->
    <el-card>
      <topcard
        ><template>
          评论管理
        </template>
      </topcard>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="title" label="文章标题"> </el-table-column>
        <el-table-column prop="total_comment_count" label="总评论数">
        </el-table-column>
        <el-table-column prop="fans_comment_count" label="粉丝评论数">
        </el-table-column>
        <el-table-column label="评论状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.comment_status ? 'primary' : 'success'">{{
              scope.row.comment_status ? '关闭' : '正常'
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.comment_status ? 'primary' : 'danger'"
              @click="componentClick(scope.row)"
              size="small"
              >{{
                scope.row.comment_status ? '打开评论' : '关闭评论'
              }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { commentsList, commentsStatus } from '@/api/comments.js'
export default {
  data () {
    return {
      tableData: [],
      total: 1000,
      page: 1
    }
  },
  methods: {
    // 点击页码选择评论页数数据
    handleCurrentChange (val) {
      this.page = val
      this.getData()
    },
    // 封装一个获取评论的方法
    getData () {
      commentsList({ page: this.page, response_type: 'comment' }).then(res => {
        // console.log(res)
        this.tableData = res.data.results
        this.total = res.data.total_count
      })
    },
    // 点击打开
    // id, comment_status
    componentClick (comment) {
      commentsStatus({
        article_id: comment.id.toString(),
        allow_comment: !comment.comment_status
      }).then(res => {
        // console.log(res)
        // 传递一个每行的数据时引用类型，这样当改变时tableData的数据也会随之改变
        comment.comment_status = res.data.allow_comment
        this.$message({
          type: 'success',
          message: '编辑成功!'
        })
      })
    }
  },
  created () {
    // 进入页面获取评论，默认第一页
    this.getData()
  }
}
</script>

<style lang="less">
.comments {
  width: 100%;
  height: 100%;
  .el-pagination {
    text-align: center;
    padding-top: 20px;
  }
}
</style>
